package com.wzg.appframe.webservice;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.JsonObject;
import com.wzg.appframe.action.BaseAction;
import com.wzg.appframe.bean.KDResponseData;
import com.wzg.appframe.net.WebServiceUtil;
import com.wzg.appframe.utils.GsonUtil;
import com.wzg.appframe.utils.TLog;
import com.wzg.appframe.utils.ToastUtil;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class WebServiceBaseTask extends AsyncTask<String, Integer, String> {
    SoapObject request;
    String namespace = "http://tempuri.org/";
    String mUrl;
    String methodName;
    BaseAction mAction;
    Map<String, Object> mMap;
    public boolean isH5Req;

    public WebServiceBaseTask(BaseAction action, String url, String methodName, Map<String, Object> args) {
        this.mUrl = url;
        this.methodName = methodName;
        this.mAction = action;
        this.mMap = args;

    }

    @Override
    protected String doInBackground(String... strings) {

        try {
            return getRemoteInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
//        s = s.replaceAll("\"","");
        TLog.d("req result=" + s);
        if (null == s || "".equals(s)) {
            ToastUtil.show("连接服务器失败 result=" + s);
            return;
        }

        try {
            JsonObject jsonObject = GsonUtil.getObject(s);
            if (isH5Req) {
//                mAction.onCallBack(methodName, jsonObject);
            } else {
                KDResponseData responseData = new KDResponseData();
                responseData.setSuccess(jsonObject.get("Success").getAsString());
                responseData.setMessage(jsonObject.get("Message").getAsString());
                if ("true".equalsIgnoreCase(responseData.getSuccess())) {
                    Object data = jsonObject.get("Data");
//                    mAction.onCallBack(methodName, data);
                } else {
                    ToastUtil.show(responseData.getMessage());
                }
            }
        } catch (Exception e) {
            ToastUtil.show(e.getMessage());
            e.printStackTrace();
        }


        mAction.activity.hideDialog();

    }

    private String getRemoteInfo() {

        WebServiceUtil util = new WebServiceUtil(this.mUrl, namespace, methodName, true);
        TLog.d("req url=" + this.mUrl);
        TLog.d("req Parmas=" + this.mMap.toString());
        Set<String> keys = this.mMap.keySet();
        for (String key : keys) {
            util.addParams(key, this.mMap.get(key).toString());
        }

        // 获取返回的结果
        String result = null;
        try {
            result = util.start();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        Log.d("tracy", "result=" + result);
        return result;

    }
}
